package com.mbigger.thriftclient;

import com.mbigger.generate.Hello;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class ThriftclientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThriftclientApplication.class, args);

        log.info("客户端启动....");
        TTransport transport = null;
        try {
            transport = new TSocket("localhost", 9898, 30000);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            Hello.Client client = new Hello.Client(protocol);
            transport.open();
            String result = client.helloString("hans");
           log.info(result);
        } catch (TTransportException e) {
            log.error(e.getMessage());
        } catch (TException e) {
            log.error(e.getMessage());
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }

}
